<?php
namespace app\admin\controller;

use app\common\controller\ContentController;
use app\common\logic\AdminLogic;
use app\common\logic\ContentLogic;
use app\common\logic\GoodsLogic;
use app\common\logic\OrderLogic;
use app\common\logic\SysLogic;
use gdbase\Tree2;
use think\facade\Validate;

/**
 * models 模型重写示例 ， 可删除
 * Class Goods
 * @package app\admin\controller
 */
class Order extends ContentController
{
    public function initialize(){
        $this->controllerName = '订单';
        $this->modelId = 4;
        parent::initialize();
        $this->modelName = 'Order';
    }

    public function index(){
        $show = input('show',1);

        $search = input('search');
        $paramWhere['where'] = [];
        if (!empty($search)) {
            if(!empty($search['time_start']) && !empty($search['time_end'])){
                $paramWhere['where'][] = ['o.create_time','BETWEEN',[strtotime($search['time_start']),strtotime($search['time_end'] . ' 23:59:59')]];
            }
            if(!empty($search['book_info'])){
                $paramWhere['where'][] = ['o.book_realname|o.book_mobile','LIKE','%'.$search['book_info'].'%'];
            }
        }

        $paramWhere['search'] = $search;

        $alj = new OrderLogic($this->modelName);

        //附加条件
        switch ($show) {
            case 6: // 全部订单

                break;
            case 5:
                $paramWhere['where'][] = ['o.status','=',0];
                break;
            case 4:
                $paramWhere['where'][] = ['o.status','=',4];
                break;
            case 3:
                $paramWhere['where'][] = ['o.status','=',1];
                break;
            case 2:
                $paramWhere['where'][] = ['o.status','=',3];
                break;
            case 1:
                $paramWhere['where'][] = ['o.status','=',2];
                break;
        }

        $pagerInfo = $this->getPagerInfo($alj->_getOrderPagerCountByWhere($paramWhere['where']),input('p',1),$paramWhere['search']);
        $dataList = $alj->_getOrderPager($paramWhere['where'],$pagerInfo['offset'],$pagerInfo['pageSize'],'id DESC');

        $this->assign('datas',$dataList);
        $this->assign('search',$paramWhere['search']);

        $this->assign($pagerInfo);
        // 通过数据源设置的变量
        $this->assign('orderSiteEnum',config('site_enum.order'));
        $this->assign('orderPayLogSiteEnum',config('site_enum.order_pay_log'));
        $this->assign('show',$show);
        return $this->fetch();
    }

    /**
     * 订单详情
     */
    public function detail()
    {
        $orderId = input('id');
        if (empty($orderId)){
            $this->error('参数错误');
        }
        $orderLogic = new OrderLogic($this->modelName);
        $orderModel = $orderLogic->_getOrderModelById($orderId);
        if (empty($orderModel)){
            $this->error('找不到订单');
        }

        $payLog = [];
        if (!empty($orderModel['pay_log']) && count($orderModel['pay_log']) > 0) {
            $payLog = $orderModel['pay_log'][0];
        }

        $this->assign('payLog', $payLog);
        $this->assign('orderSiteEnum',config('site_enum.order'));
        $this->assign('goodsOptionSpecsVal',config('site.goods_option_specs_val'));
        $this->assign('orderPayLogSiteEnum',config('site_enum.order_pay_log'));
        $this->assign('orderModel', $orderModel);
//        dump($orderModel);exit;
        return $this->fetch();
    }

    public function recovery(){
        $goodsLogic = new GoodsLogic($this->modelName);
        $id = input('param.id');
        if (empty($id)){
            $this->error('参数错误',url('index'));
        }
        $id = hashIdDecode($id);
        if (empty($id)){
            $this->error('参数错误',url('index'));
        }

        $jieguo = $goodsLogic->_recoveryGoodsById($id);

        if($jieguo !== false){
            $this->success('恢复成功');
        }
        else{
            $this->error('恢复失败');
        }
    }

    /**
     * 改价
     * @return mixed
     */
    public function change_price(){
        $orderLogic = new OrderLogic($this->modelName);
        if (request()->isAjax()) {
            $postData = input('post.');
            $jieguo = $orderLogic->changeOrderPrice($postData['id'], $this->adminId, $postData);
            if ($jieguo['err_code'] != 0) {
                $this->error($jieguo['msg']);
            }
            else{
                $this->success('操作成功');
            }
        }
        $orderId = input('id');
        if (empty($orderId)){
            $this->error('参数错误');
        }
        $orderModel = $orderLogic->_getOrderModelById($orderId);
        if (empty($orderModel)){
            $this->error('找不到订单');
        }

        $this->assign('orderModel', $orderModel);
        return $this->fetch();
    }

    /**
     * 修改订单，备注、状态等
     * @return mixed
     */
    public function change_order(){
        $type = input('type','seller_remark');
        $orderLogic = new OrderLogic($this->modelName);
        if (request()->isAjax()) {
            $postData = input('post.');
            $jieguo = $orderLogic->backChangeOrder($postData['id'], $this->adminId, $postData);
            if ($jieguo['err_code'] != 0) {
                $this->error($jieguo['msg']);
            }
            else{
                $this->success('操作成功');
            }
        }
        $orderId = input('id');
        if (empty($orderId)){
            $this->error('参数错误');
        }
        $orderModel = $orderLogic->_getOrderModelById($orderId);
        if (empty($orderModel)){
            $this->error('找不到订单');
        }

        $this->assign('type', $type);
        $this->assign('orderModel', $orderModel);

        if ('send' == $type || 'modify_send' == $type) {
            $sysLogic = new SysLogic();
            $this->assign('expressList', $sysLogic->_getExpressList());
        }

        return $this->fetch('order/change_order');
    }

    /**
     * 确认付款
     */
    public function confirm_pay()
    {
        if (!request()->isAjax()) {
            $this->error('提交方法错误');
        }
        $orderLogic = new OrderLogic($this->modelName);
        $orderId = input('id');
        if (empty($orderId)) {
            $this->error('参数错误');
        }
        $jieguo = $orderLogic->backOrderConfirmPay($orderId, $this->adminId);
        if ($jieguo['err_code'] != 0) {
            $this->error($jieguo['msg']);
        }
        else{
            $this->success('操作成功');
        }
    }

    /**
     * 关闭订单
     */
    public function close_order()
    {
        if (!request()->isAjax()) {
            $this->error('提交方法错误');
        }
        $orderLogic = new OrderLogic($this->modelName);
        $orderId = input('id');
        if (empty($orderId)) {
            $this->error('参数错误');
        }
        $jieguo = $orderLogic->backOrderCloseOrder($orderId, $this->adminId);
        if ($jieguo['err_code'] != 0) {
            $this->error($jieguo['msg']);
        }
        else{
            $this->success('操作成功');
        }
    }

    /**
     * 查看物流
     */
    public function show_express()
    {
        $express = input('express');
        $expressSn = input('express_sn');
        if(empty($expressSn)){
            $this->error('运单号不能为空');
        }

        $sysLogic = new SysLogic();

        $jieguo = $sysLogic->showExpress($expressSn,$express);
        if ($jieguo['err_code'] != 0) {
            $this->error($jieguo['msg']);
        }
        $expressList = $jieguo['data'];
        $this->assign('expressList', $expressList);
        return $this->fetch();
    }

    /**
     * 修改物流
     */
    public function modify_express(){
        return $this->change_order();
    }

    /**
     * 取消发货
     * @return mixed
     */
    public function cancel_express(){
        return $this->change_order();
    }

    /**
     * 确认收货
     */
    public function confirm_finish()
    {
        if (!request()->isAjax()) {
            $this->error('提交方法错误');
        }
        $orderLogic = new OrderLogic($this->modelName);
        $orderId = input('id');
        if (empty($orderId)) {
            $this->error('参数错误');
        }
        $jieguo = $orderLogic->backOrderConfirmFinish($orderId, $this->adminId);
        if ($jieguo['err_code'] != 0) {
            $this->error($jieguo['msg']);
        }
        else{
            $this->success('操作成功');
        }
    }
}
